import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import qs from 'qs'

const planUrl = CommonConfig.planningUrl + '/v1'
const reportUrl = CommonConfig.reportUrl + '/v1'
const planningUrl = CommonConfig.planningUrl + '/v2'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

/**
 * 获取质检单列表
 * keyword 关键字
 * pageNum 页码（从 1 起计）
 * pageSize 每页条数
 * searchBy 搜索依据 [workOrderNum]
 * showFlag 是否显示已关闭质检单 [true/false]
 * sortBy 排序依据 [workOrderNum/createTime/status]
 * value 排序值 [asc/desc]
 **/
export function getQualityOrders (query) {
  const params = {
    ...query,
    isPersonalComputer: true
  }
  const newUrl = planUrl + '/quality_control_orders:list'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 根据质检工单ID 列举质检原因
 * id 质检工单ID
 * isCompromiseCause 是否是让步原因
 **/
export function getQualityCauses (query) {
  const reqData = {
    params: {
      isCompromiseCause: query.isCompromiseCause || false,
      isQualityControl: true,
      id: query.id || undefined
    }
  }
  const newUrl = planUrl + `/orders/${query.id}/quality_control_causes`
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

export function getQualitySubCauses (inputType, subOrderId) {
  const reqData = {
    params: {
      inputType
    }
  }
  const newUrl = planUrl + `/quality_control_orders/sub_orders/${subOrderId}/causes:getGeneralInfo`
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res.data))
  })
}

/**
 * 质检原因录入
 * caused 原因代码
 * count 数量
 * deviceId 设备ID
 * gid  原因类型ID 让步原因null
 * isQualityControl
 * label 原因名称
 * orderId  质检工单ID
 * shiftsId 班制ID
 * remark 说明
 **/
export function addQualityCauses (query) {
  const config = {
    params: {
      causeId: query.causeId || undefined,
      count: query.count || undefined,
      label: query.label || undefined,
      deviceId: query.deviceId || undefined,
      isQualityControl: true,
      orderId: query.orderId || undefined,
      gid: query.gid || undefined,
      shiftsId: query.shiftsId || undefined,
      remark: query.remark || undefined
    }
  }
  const newUrl = planUrl + '/quality_control_causes:admin'
  return axiosInstance.post(newUrl, config.params).then((res) => {
    return Promise.resolve((res))
  })
}
export function addQualitySubCauses (query, id, subId, isAdmin) {
  let newUrl = `${isAdmin ? planUrl : planningUrl}/quality_control_orders/${id}/sub_orders/${subId}/causes`

  isAdmin && (newUrl = `${newUrl}?isAdmin=true`)
  return axiosInstance.post(newUrl, query).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 质检原因编辑
 * code 原因代码
 * count 数量
 * gid  原因类型ID 让步原因null
 * label 原因名称
 * qualityControlOrderId  质检工单ID
 * shiftsId 班制ID
 * shiftsModeId 班制模式ID
 * shiftsName 班制名称
 **/
export function editQualityCauses (query) {
  const params = {
    causeId: query.causeId || undefined,
    count: query.count || undefined,
    gid: query.gid || undefined,
    label: query.label || undefined,
    remark: query.remark,
    shiftsId: query.shiftsId || undefined,
    shiftsModeId: query.shiftsModeId || undefined,
    shiftsName: query.shiftsName || undefined
  }
  const newUrl = planUrl + `/quality_control_causes/${query.orderId}`
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
export function editQualitySubCauses (query, id) {
  const newUrl = planUrl + `/quality_control_orders/sub_orders/causes/${id}`
  return axiosInstance.put(newUrl, query).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 删除质检原因
 * label 原因名称
 * id  质检原因ID
 **/
export function delQualityCauses (query) {
  const reqData = {
    params: {
      label: query.label || undefined
    }
  }
  const newUrl = planUrl + `/quality_control_orders/sub_orders/causes/${query.id}`
  return axiosInstance.delete(newUrl, reqData)
}
/**
 * 根据质检工单ID 列举质检原因记录
 * id  质检工单ID
 **/
export function getQualityLogs (query) {
  const newUrl = planUrl + `/quality_control_orders/${query.id}/quality_control_order_logs`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 关闭质检单
 * id  质检工单ID
 **/
export function closeQualityOrder (query) {
  const newUrl = planUrl + `/quality_control_orders/${query.id}/close`
  return axiosInstance.put(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 批量创建质检原因
 * causeId 原因Id·
 * count 数量·
 * gid  原因类型ID 让步原因null·
 * gitName 备注
 * label 原因名称
 * orderId  质检工单ID·
 * shiftsId 班制ID·
 * shiftsModeId 班制模式ID·
 * shiftsName 班制名称·
 */
export function batchCreateQuality (query) {
  let config = {
    params: query,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = planUrl + '/quality_control_causes:batchCreate'
  return axiosInstance.post(url, config.params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 列举日产量报表
 * @param params
 */
export function getYieldReportDaily (params) {
  let config = {
    params: params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = reportUrl + '/yield/yield_report_daily'
  return axiosInstance.get(url, config).then((res) => {
    return Promise.resolve((res))
  })
}

export function getYieldReportDailyExcel (params) {
  let config = {
    params: params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = reportUrl + '/yield/yield_report_daily/excel:export'
  return axiosInstance.get(url, config).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 列举日产量报表
 * @param params
 */
export function getYieldReportWeek (params) {
  let config = {
    params: params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = reportUrl + '/yield/yield_report_week'
  return axiosInstance.get(url, config).then((res) => {
    return Promise.resolve((res))
  })
}

export function getYieldReportWeekExcel (params) {
  let config = {
    params: params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = reportUrl + '/yield/yield_report_week/excel:export'
  return axiosInstance.get(url, config).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 列举月产量报表
 * @param params
 */
export function getYieldReportMonth (params) {
  let config = {
    params: params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = reportUrl + '/yield/yield_report_month'
  return axiosInstance.get(url, config).then((res) => {
    return Promise.resolve((res))
  })
}

export function getYieldReportMonthExcel (params) {
  let config = {
    params: params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = reportUrl + '/yield/yield_report_month/excel:export'
  return axiosInstance.get(url, config).then((res) => {
    return Promise.resolve((res))
  })
}
